<?php

/**
 * This is the model class for table "{{address}}".
 *
 * The followings are the available columns in table '{{address}}':
 * @property integer $_id
 * @property integer $parent_id
 * @property string $name
 * @property string $fullname
 * @property string $value
 * @property string $description
 * @property string $profile
 * @property integer $resource_id
 * @property string $resource_class
 * @property string $value_type
 * @property integer $space_id
 * @property integer $app_id
 * @property integer $status
 * @property string $data
 */
class BWMAddress extends BWActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{address}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name', 'required'),
            array('parent_id, resource_id, space_id, app_id, status', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 127),
            array('fullname, description', 'length', 'max' => 255),
            array('value', 'length', 'max' => 256),
            array('resource_class', 'length', 'max' => 128),
            array('value_type', 'length', 'max' => 20),
            array('profile, data', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('_id, parent_id, name, fullname, value, description, profile, resource_id, resource_class, value_type, space_id, app_id, status, data', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            '_id' => 'ID',
            'parent_id' => 'Parent',
            'name' => 'Name',
            'fullname' => 'Fullname',
            'value' => 'Value',
            'description' => 'Description',
            'profile' => 'Profile',
            'resource_id' => 'Resource',
            'resource_class' => 'Resource Class',
            'value_type' => 'Value Type',
            'space_id' => 'Space',
            'app_id' => 'App',
            'status' => 'Status',
            'data' => 'Data',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('_id', $this->_id);
        $criteria->compare('parent_id', $this->parent_id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('fullname', $this->fullname, true);
        $criteria->compare('value', $this->value, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('profile', $this->profile, true);
        $criteria->compare('resource_id', $this->resource_id);
        $criteria->compare('resource_class', $this->resource_class, true);
        $criteria->compare('value_type', $this->value_type, true);
        $criteria->compare('space_id', $this->space_id);
        $criteria->compare('app_id', $this->app_id);
        $criteria->compare('status', $this->status);
        $criteria->compare('data', $this->data, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * @return CDbConnection the database connection used for this class
     */
    public function getDbConnection() {
        //return Yii::app()->mydb2;
        return parent::getDbConnection();
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return BWMAddress the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

}
